package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;

public class DianPingAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public DianPingAdapter(Activity activity, List<JSONObject> itemList) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public RatingBar bar;
		public TextView day,month,title,intro,nomore;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int dianping_item = R.layout.dianping_item;
			convertView = LayoutInflater.from(appContext).inflate(dianping_item, null);
			holder.day = (TextView) convertView.findViewById(R.id.dianping_item_day);
			holder.month = (TextView) convertView.findViewById(R.id.dianping_item_month);
			holder.title = (TextView) convertView.findViewById(R.id.dianping_item_title);
			holder.intro = (TextView) convertView.findViewById(R.id.dianping_item_intro);
			holder.nomore = (TextView) convertView.findViewById(R.id.dianping_item_nomore);
			holder.bar = (RatingBar) convertView.findViewById(R.id.dianping_item_bar);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		try {
			holder.nomore.setVisibility(position==itemList.size()-1&&pageEnd?View.VISIBLE:View.GONE);
			JSONObject json = itemList.get(position);
			holder.day.setText(BaseTools.getFrom("content.dd", json));
			holder.month.setText("/"+BaseTools.getFrom("content.MM", json));
			holder.title.setText(BaseTools.getFrom("entry.name", json));
			holder.intro.setText(BaseTools.getFrom("content.text", json));
			holder.bar.setRating(Float.parseFloat(BaseTools.getFrom("content.score", json)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
